﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;
using DevExpress.XtraLayout.Utils;

namespace EFong.View.TrainTicket
{
    public partial class Frm_TrainTicketDetailEdit : BasicForm
    {
        public const string TRAIN_LOCATION_HK = "九龙红磡";
        public const string TRAIN_LOCATION_GZ = "广州东";

        private Proxy.BaseServiceProxy _baseProxy {get{ return Proxy.BaseServiceProxy.GetInstance();}}

        public Frm_TrainTicketDetailEdit()
        {
            InitializeComponent();
        }

        protected override void InitFields()
        {
        }

        protected override void Events()
        {
            this.spe_DeliveryCost.ValueChanged += (s, e) => { updateTotalFields(); };
            this.spe_Qty.ValueChanged += (s, e) => { updateTotalFields(); };
            this.spe_TicketPrice.ValueChanged += (s, e) => { updateTotalFields(); };
            this.spe_VendorPrice.ValueChanged += (s, e) => { updateTotalFields(); };

            this.ckb_IsToHK.CheckedChanged += (s, e) => { updateIsToHK(); };
            this.rad_Back.CheckedChanged += (s, e) => { updateLocation(); };
            this.rad_Go.CheckedChanged += (s, e) => { updateLocation(); };
        }

        protected override void LoadDatas()
        {
            updateIsToHK();
        }

        private void updateIsToHK()
        {
            bool isToHK = this.ckb_IsToHK.GetValue();

            this.txt_DepartLocation.ReadOnly(isToHK);
            this.txt_Destination.ReadOnly(isToHK);

            this.rad_Back.ReadOnly(!isToHK);
            this.rad_Go.ReadOnly(!isToHK);

            this.layoutControlItem2.Visibility = this.layoutControlItem3.Visibility = isToHK ? LayoutVisibility.Always : LayoutVisibility.Never;

            this.lct_VendorPrice.Text = isToHK ? "结算价" : "工本费";
        }

        private void updateLocation()
        {
            if (rad_Go.GetValue())
            {
                if (txt_DepartLocation.GetText() != TRAIN_LOCATION_GZ)
                    txt_DepartLocation.SetText(TRAIN_LOCATION_GZ);

                if (txt_Destination.GetText() != TRAIN_LOCATION_HK)
                    txt_Destination.SetText(TRAIN_LOCATION_HK);
            }
            else
            {
                if (txt_DepartLocation.GetText() != TRAIN_LOCATION_HK)
                    txt_DepartLocation.SetText(TRAIN_LOCATION_HK);

                if (txt_Destination.GetText() != TRAIN_LOCATION_GZ)
                    txt_Destination.SetText(TRAIN_LOCATION_GZ);
            }
        }

        private void updateTotalFields()
        {
            Contract.TrainTicketOrderDetail detail = getDetail();

            this.txt_TotalPrice.SetText(detail.TotalPrice);
            this.txt_TotalProfit.SetText(detail.Profit);
            this.txt_TotalVendorPrice.SetText(detail.TotalVendorPrice);
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Save":
                    this.save();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private void save()
        {
            Contract.TrainTicketOrderDetail detail = null;

            UIOperManager.SaveData(
                () =>
                {
                    return true;
                },
                () =>
                {
                    detail = getDetail();
                    updateToDetail(detail);
                    return true;
                },
                string.Empty,
                () =>
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                });
        }

        private Contract.TrainTicketOrderDetail _detail;
        public Contract.TrainTicketOrderDetail Detail
        {
            get { return _detail; }
            set
            {
                _detail = value;
                onDetailChanged();
            }
        }

        private void onDetailChanged()
        {
            if (Detail == null)
            {

            }
            else
            {
                this.dte_DepartDate.SetValue(Detail.DepartDate);
                this.txt_DepartTime.SetText(Detail.出发时间);
                this.txt_DepartLocation.SetText(Detail.DepartLocation);
                this.txt_Destination.SetText(Detail.Destination);

                this.txt_TrainNo.SetText(Detail.TrainNo);
                this.cmb_SeatType.SetValue(Detail.SeatType);
                this.spe_TicketPrice.SetValue(Detail.TicketPrice);
                this.spe_VendorPrice.SetValue(Detail.VendorPrice);
                this.spe_DeliveryCost.SetValue(Detail.DeliveryCost);
                this.spe_Qty.SetValue(Detail.Qty);

                if (Detail.IsToHK && Detail.DepartLocation == TRAIN_LOCATION_GZ)
                {
                    this.rad_Go.SetValue(true);
                }
                else if (Detail.IsToHK && Detail.DepartLocation == TRAIN_LOCATION_HK)
                {
                    this.rad_Back.SetValue(true);
                }

                this.ckb_IsToHK.SetValue(Detail.IsToHK);

                updateTotalFields();
            }
        }

        private Contract.TrainTicketOrderDetail getDetail()
        {
            Contract.TrainTicketOrderDetail detail = new Contract.TrainTicketOrderDetail();

            DateTime? departDate = this.dte_DepartDate.GetValue();
            //DateTime departTime = this.dte_DepartTime.GetValue().Value;

            detail.DepartDate = departDate;
            detail.出发时间 = this.txt_DepartTime.GetText();
            detail.DepartLocation = this.txt_DepartLocation.GetText();
            detail.Destination = this.txt_Destination.GetText();

            detail.TrainNo = this.txt_TrainNo.GetText();
            detail.SeatType = this.cmb_SeatType.GetDisplay();
            detail.TicketPrice = this.spe_TicketPrice.GetValue();
            detail.VendorPrice = this.spe_VendorPrice.GetValue();
            detail.DeliveryCost = this.spe_DeliveryCost.GetValue();
            detail.Qty = (int)this.spe_Qty.GetValue();
            detail.IsToHK = this.ckb_IsToHK.GetValue();

            if (Detail != null)
                detail.TrainTicketOrder = Detail.TrainTicketOrder;

            return detail;
        }

        private void updateToDetail(Contract.TrainTicketOrderDetail detail)
        {
            Detail.DepartDate = detail.DepartDate;
            Detail.出发时间 = detail.出发时间;
            Detail.DepartLocation = detail.DepartLocation;
            Detail.Destination = detail.Destination;

            Detail.TrainNo = detail.TrainNo;
            Detail.SeatType = detail.SeatType;
            Detail.TicketPrice = detail.TicketPrice;
            Detail.VendorPrice = detail.VendorPrice;
            Detail.DeliveryCost = detail.DeliveryCost;
            Detail.Qty = detail.Qty;
            Detail.IsToHK = detail.IsToHK;
        }


        protected override void OnViewStateChanged()
        {
            switch (ViewState)
            {
                case ViewStates.EDIT:
                    if (!AppParameters.IsAdRole)
                    {
                        spe_TicketPrice.ReadOnly(true);
                        spe_Qty.ReadOnly(true);
                        spe_VendorPrice.ReadOnly(true);
                        spe_DeliveryCost.ReadOnly(true);
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
